<template>
  <div></div>
</template>

<script>
export default {
  name: "ToES",
  data() {
    return {
      ue: undefined
    }
  },
  created() {
    if (typeof this.ue != "object") this.ue = {}
  },
  mounted() {
    console.log();
  },
  methods: {
    register(callback, timeout) {
      if (typeof callback != "function") return "";
      var uuid = this.uuidv4;
      this.ue.interface[uuid] = callback;
      setTimeout(function () {
            delete this.ue.interface[uuid];
          },
          1000 * Math.max(1, parseInt(timeout) || 0));

      return uuid;
    },
    getUe4(){
      if (typeof this.ue.interface != "object" || typeof this.ue.interface.broadcast != "function") {
        // mobile
        this.ue.interface = {};
        return  (name, data, callback, timeout)=> {
          if (typeof name != "string") return;

          if (typeof data == "function") {
            timeout = callback;
            callback = data;
            data = null;
          }

          let uuid = this.register(callback, timeout);
          let args = [name, "", uuid];
          if (typeof data != "undefined") args[1] = data;

          let hash = encodeURIComponent(JSON.stringify(args));
          if (typeof history == "object" && typeof history.pushState == "function") {
            history.pushState({}, "", "#" + hash);
            history.pushState({}, "", "#" + encodeURIComponent("[]"));
          } else {
            document.location.hash = hash;
            document.location.hash = encodeURIComponent("[]");
          }
        };
      } else {
        return  (Interface)=>{
          // desktop
          this.ue.interface = {};
          return function (name, data, callback, timeout) {
            if (typeof name != "string")
              return;

            if (typeof data == "function") {
              timeout = callback;
              callback = data;
              data = null;
            }
            let uuid = this.register(callback, timeout);
            if (typeof data != "undefined")
              Interface.broadcast(name, JSON.stringify(data), uuid);
            else
              Interface.broadcast(name, "", uuid);
          };
        }
      }
    }
  },
  computed: {
    uuidv4(){
      return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (c)=> {
        return (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
      });
    }
  },
}
</script>

<style scoped>

</style>